<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客列表页</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/blog_list.css">

</head>
<body>
    <!--导航栏-->
    <div class="nav">
        <img src="image/log1.png" alt="">
        <span>我的博客系统</span>
        <!--这个div仅用于占位-->
        <div class="zhan"></div>

        <a href="blog_list.html">主页</a>
        <a href="blog_edit.html">写博客</a>
        <a href="logout">注销</a>
    </div>
    
    <!--页面主体-->
    <div class="container">
        <!--左侧信息-->
        <div class="container-left">

            <!--左侧用户信息-->
            <div class="card">

                <!--头像-->
                <img src="image/头像.png" alt="">

                <!--用户名-->
                <h3>小比特</h3>

                <a href="#">github地址</a>

                <div class="row">
                    <span>文章</span>
                    <span>分类</span>
                </div>
                <div class="row">
                    <span>2</span>
                    <span>1</span>
                </div>

            </div>

        </div>

        <!--右侧-->
        <div class="container-right">

            <!--一篇博客-->
            
        </div>
    </div>
    <script src="js/jquery.min.js"></script>
    <script>
        //在页面加载的时候向服务器获取数据
        function getBlogs(){
            $.ajax({
                type: 'get',
                url: 'blog',
                success: function(body) {
                    let containerRight = document.querySelector('.container-right');
                    for (let blog of body) {
                        
                        //构造页面内容
                        //构造整个博客的div
                        let blogDiv = document.createElement('div');
                        blogDiv.className = 'blog';

                        //构造标题
                        let titleDiv = document.createElement('div');
                        titleDiv.className = 'title';
                        titleDiv.innerHTML = blog.title;
                        blogDiv.appendChild(titleDiv);

                        //构造发布时间
                        let dataDiv = document.createElement('div');
                        dataDiv.className = 'data';
                        dataDiv.innerHTML = blog.postTime;
                        blogDiv.appendChild(dataDiv);

                        //构造全文摘要
                        let descDiv = document.createElement('div');
                        descDiv.className = 'desc';
                        descDiv.innerHTML = blog.content;
                        blogDiv.appendChild(descDiv);

                        //查看全文
                        let a = document.createElement('a');
                        a.innerHTML = '查看全文 &gt;&gt;';
                        a.href = 'blog_detail.html?blogId=' + blog.blogId;
                        blogDiv.appendChild(a);

                        //把blogDiv添加到父元素中
                        containerRight.appendChild(blogDiv);
                    }
                }
            });
        }
        getBlogs();
        
        function checkLogin(){
            $.ajax({
                type: 'get',
                url: 'login',
                success: function(body){
                    if(body.userId && body.userId > 0){
                        //存在
                        console.log("当前用户已登录");

                        let h3 = document.querySelector('.container-left .card h3');
                        h3.innerHTML = body.username;
                    }else{
                        //登陆失败
                        location.assign('login.html');
                    }
                }
            });
        }
        checkLogin();
    </script>
</body>
</html>